package com.star.area.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.star.area.dao.CountryAssociatedInfoEntityRepository;
import com.star.area.dao.model.CountryAssociatedInfoEntity;
import com.star.area.service.CountryAssociatedInfoService;

@Component
public class CountryAssociatedInfosServiceImpl implements CountryAssociatedInfoService {
	private static Logger log = LoggerFactory.getLogger(CountryAssociatedInfosServiceImpl.class);
	@Autowired
	private CountryAssociatedInfoEntityRepository countryAssociatedInfoEntityRepository;
	
	public int dataCacheTimeout = 3600;  //单位秒
	public static final String TIME_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
    /**
     	* 获取全部国家相关数据
     **/
    LoadingCache<String, Optional<List<CountryAssociatedInfoEntity>>> countryAssociatedInfoCahce = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(dataCacheTimeout, TimeUnit.SECONDS).refreshAfterWrite(dataCacheTimeout/6, TimeUnit.SECONDS)
            .recordStats().removalListener((RemovalListener<String,Optional<List<CountryAssociatedInfoEntity>>>) notification -> {
            	log.warn("CountryAssociatedInfosServiceImpl|countryAssociatedInfoCahceByCountryId|remove key : " + notification.getKey() + ", value : " + notification.getValue() + ", cause  " + notification.getCause());
            })
            .build(new CacheLoader<String,Optional<List<CountryAssociatedInfoEntity>>>(){
				@Override
				public Optional<List<CountryAssociatedInfoEntity>> load(String all) throws Exception {
					List<CountryAssociatedInfoEntity> countryAssocitedInfoList = countryAssociatedInfoEntityRepository.findAll();
					return Optional.ofNullable(countryAssocitedInfoList);
				}
            	
            });
   
	@Override
	public CountryAssociatedInfoEntity getByFKCountryId(Long fkCountryId) {
		if(null==fkCountryId) {
			return null;
		}
		try {
			List<CountryAssociatedInfoEntity> countryAssocitedInfos = countryAssociatedInfoCahce.get("all").get();
			long counrtyId =fkCountryId;
			CountryAssociatedInfoEntity countryAssocitedInfo = countryAssocitedInfos.stream().filter(countryInfo->countryInfo.getFkCountryId()==counrtyId).findAny().orElse(null);
			return countryAssocitedInfo;
		} catch (Exception e) {
			log.error("CountryAssociatedInfosServiceImpl|getByFKCountryId|fkCountryId={},error:{}",fkCountryId,e);
		}
		return null;
	}

	@Override
	public CountryAssociatedInfoEntity getCode(String code) {
		try {
			List<CountryAssociatedInfoEntity> countryAssocitedInfos = countryAssociatedInfoCahce.get("all").get();
			CountryAssociatedInfoEntity countryAssocitedInfo = countryAssocitedInfos.stream().filter(countryInfo->countryInfo.getCode().equals(code)).findAny().orElse(null);
			return countryAssocitedInfo;
		} catch (Exception e) {
			log.warn("CountryAssociatedInfosServiceImpl|getCode|error:,code={},error:{}",code,e);
		}
		return null;
	}

	@Override
	public List<CountryAssociatedInfoEntity> getPortalOnline() {
		try {
			List<CountryAssociatedInfoEntity> allCountryAssociatedInfo = countryAssociatedInfoCahce.get("all").get();
			List<CountryAssociatedInfoEntity> portalOnlineCountryAssociatedInfo = allCountryAssociatedInfo.stream().filter(countryInfo->countryInfo.isPortalOnline()).sorted(Comparator.comparing(CountryAssociatedInfoEntity::getFkCountryId)).collect(Collectors.toList());
			return portalOnlineCountryAssociatedInfo;
		} catch (Exception e) {
			log.warn("CountryAssociatedInfosServiceImpl|getPortalOnline|error:{}",e);
		}
		return null;
	}

	@Override
	public List<CountryAssociatedInfoEntity> getAll() {
		try {
			List<CountryAssociatedInfoEntity> allCountryAssociatedInfo = countryAssociatedInfoCahce.get("all").get();
			return allCountryAssociatedInfo;
		} catch (Exception e) {
			log.warn("CountryAssociatedInfosServiceImpl|getAll|error:{}",e);
		}
		return null;
	}

	@Override
	public List<CountryAssociatedInfoEntity> getAllOnLine() {
		try {
			List<CountryAssociatedInfoEntity> allCountryAssociatedInfo = countryAssociatedInfoCahce.get("all").get();
			allCountryAssociatedInfo = allCountryAssociatedInfo.stream().filter(countryInfo->countryInfo.isOnLine()).sorted(Comparator.comparing(CountryAssociatedInfoEntity::getFkCountryId)).collect(Collectors.toList());
			return allCountryAssociatedInfo;
		} catch (Exception e) {
			log.warn("CountryAssociatedInfosServiceImpl|getAllOnLine|error:{}",e);
		}
		return null;
	}

	@Override
	public List<CountryAssociatedInfoEntity> getBossOnline() {
		try {
			List<CountryAssociatedInfoEntity> allCountryAssociatedInfo = countryAssociatedInfoCahce.get("all").get();
			allCountryAssociatedInfo = allCountryAssociatedInfo.stream().filter(countryInfo->countryInfo.isBossStatus()).collect(Collectors.toList());
			return allCountryAssociatedInfo;
		} catch (Exception e) {
			log.warn("CountryAssociatedInfosServiceImpl|getBossOnline|error:{}",e);
		}
		return null;
	}

	@Override
	public List<CountryAssociatedInfoEntity> getByFKCountryIds(List<Long> fkCounrtyIds) {
		try {
			List<CountryAssociatedInfoEntity> allCountryAssociatedInfo = countryAssociatedInfoCahce.get("all").get();
			allCountryAssociatedInfo = allCountryAssociatedInfo.stream().filter(countryInfo->fkCounrtyIds.contains(countryInfo.getFkCountryId())).collect(Collectors.toList());
			return allCountryAssociatedInfo;
		} catch (Exception e) {
			log.warn("CountryAssociatedInfosServiceImpl|getByFKCountryIds|error:{}",e);
		}
		return null;
	}
}